package com.bdc.whatwhat.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.views.cell.HeaderCell;
import com.bdc.whatwhat.views.cell.ViewCell;
import com.bdc.whatwhat.views.cell.WhatWhatCell;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gfimac on 21/07/2014.
 */
public class WhatWhatFluxAdapter extends BaseAdapter{

    Context context;

    Whatter whatter;
    List<WhatWhat> whatWhatList;

    HeaderCell header;
    View headerView;

    FaireWhatWhatTask.FaireWhatWhatTaskDelegate faireWhatWhatTaskDelegate;

    WhatWhatFluxAdapterListener listener;

    public WhatWhatFluxAdapter(Context context, Whatter whatter, List<WhatWhat> whatWhatList, FaireWhatWhatTask.FaireWhatWhatTaskDelegate faireWhatWhatTaskDelegate) {
        this.context = context;
        this.whatWhatList = whatWhatList;
        this.whatter = whatter;
        this.faireWhatWhatTaskDelegate = faireWhatWhatTaskDelegate;
    }

    public WhatWhatFluxAdapterListener getListener() {
        return listener;
    }

    public void setListener(WhatWhatFluxAdapterListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0)
            return 0;
        else
            return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        if(whatWhatList == null)
            return 0;
        else
            return whatWhatList.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        if(i == 0){
            return whatter;
        }else{
            if( whatWhatList == null)
                return  null;
            else {
                return whatWhatList.get(i - 1);
            }
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View reuseView, ViewGroup viewGroup){

        View view = reuseView;

        int apparition = 0;

        if(i == 0){

            if(headerView == null) {
                headerView = View.inflate(context, R.layout.cell_header, null);
            }

            if(header == null){
                header = new HeaderCell();
                header.construire(context, headerView, (Whatter) getItem(i), i);
            }
            else
                header.construire(headerView);

            apparition = header.apparaitre();

            view = headerView;
        }else{
            if(view == null) {
                view = View.inflate(context, R.layout.cell_whatwhat, null);
            }

            WhatWhatCell cell = (WhatWhatCell) view.getTag();

            if(cell == null){
                cell = new WhatWhatCell();
                ((WhatWhatCell)cell).setFaireWhatWhatTaskDelegate(this.faireWhatWhatTaskDelegate);
                cell.construire(context, view, (WhatWhat) getItem(i), i);
                view.setTag(cell);
            }else{
                cell.construireObject((WhatWhat) getItem(i),i);
            }

            apparition = cell.apparaitre();

        }

        if (apparition >= 1 && listener != null && getCount() > 4 && i >= (getCount() - 3)){
            //listener.onWhatWhatLoadMore();
        }

        return view;
    }

    public interface WhatWhatFluxAdapterListener {
        public void onWhatWhatLoadMore();
    }

    public void resetHeader(){
        header = null;
    }
}
